#include "Stack.h"

void StackInit(Stack* ps)
{
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->capacity == 0)
	{
		ps->a = (STDataType*)malloc(NN * sizeof(STDataType));
		if (ps->a == NULL)
		{
			exit(-1);
		}
		ps->capacity = NN;
	}
	if ((ps->top == ps->capacity)&&(ps->capacity!=0))
	{
		STDataType* poin = NULL;
		poin = (STDataType*)realloc(ps->a,2*(ps->capacity)*sizeof(STDataType));
		if (poin == NULL)
		{
			exit(-1);
		}
		ps->a = poin;
		ps->capacity = 2 * (ps->capacity);
	}
	ps->a[ps->top] = data;
	ps->top++;
}

void pprint(Stack* ps)
{
	assert(ps);
	int m = ps->top;
	while ((ps->top - 1) >= 0)
	{
		ps->top--;
		printf("%d ", ps->a[ps->top]);
	}
	printf("\n");
	ps->top = m;
}

void StackPop(Stack* ps)
{
	assert(ps);
	if (ps->top != 0)
	{
		ps->top--;
	}
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	if (ps->top != 0)
	{
		return ps->a[ps->top-1];
	}
	else
	{
		return -1;
	}
}


int StackSize(Stack* ps)
{
	assert(ps);
	if (ps->top == 0)
	{
		return 0;
	}
	else
	{
		return ps->top;
	}
}

int StackEmpty(Stack* ps)
{
	assert(ps);
	if (ps->top)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

void StackDestory(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}